package four;

import java.io.InputStream;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;

import org.apache.commons.modeler.ManagedBean;
import org.apache.commons.modeler.Registry;

import com.sun.jdmk.comm.HtmlAdaptorServer;
/**
 * 用Apache的commons-modeler来辅助开发JMX  
 *
 */
public class HelloAgent {
	
	public static void main(String[] args) throws Exception {
		
		//基于xml中的信息构建一个Registry
		Registry registry = Registry.getRegistry(null,null);
		InputStream stream = HelloAgent.class.getResourceAsStream("mbeans-descriptors.xml");
		registry.loadMetadata(stream);
		stream.close();
		
		//通过Registry获取一个MBeanServer服务
		MBeanServer server = registry.getMBeanServer();
		//得到Hello在描述文件中的信息类，对应xml文件中<mbean>标签的name属性名
		ManagedBean managed = registry.findManagedBean("Hello");
		//创建ObjectName
		ObjectName helloName = new ObjectName(managed.getDomain()+":name=HelloWorld");
		//得到ModelMBean
		ModelMBean hello = managed.createMBean(new Hello());
		//注册MBean
		server.registerMBean(hello, helloName);
		
		ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");
		HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
		server.registerMBean(adaptor, adapterName);
		adaptor.start();
		
		System.out.println("start.........................");
	}

}
